<template>
  <el-row>
    <el-col :span="12">
      <!-- <basic-container> -->
      <el-card class="box-card">
        <div slot="header" class="clearfix header-div">
          <span>业务字典列表</span>
        </div>
        <div class="clearfix">
          <avue-crud
            :option="optionParent"
            :table-loading="loading"
            :data="dataParent"
            :page="pageParent"
            ref="crud"
            v-model="formParent"
            :before-open="beforeOpen"
            @row-del="rowDel"
            @row-update="rowUpdate"
            @row-save="rowSave"
            @row-click="handleRowClick"
            @search-change="searchChange"
            @search-reset="searchReset"
            @selection-change="selectionChange"
            @current-change="currentChange"
            @size-change="sizeChange"
            @refresh-change="refreshChange"
            @on-load="onLoadParent"
          >
            <template slot="menuLeft">
              <el-button
                type="danger"
                size="small"
                icon="el-icon-delete"
                plain
                @click="handleDelete"
                >删 除
              </el-button>
            </template>
          </avue-crud>
        </div>
      </el-card>
      <!-- </basic-container> -->
    </el-col>
    <el-col :span="12">
      <!-- <basic-container> -->
      <el-card class="box-card">
        <div slot="header" class="clearfix header-div">
          <span>{{ dictValue }} 业务字典详情</span>
        </div>
        <div class="clearfix">
          <avue-crud
            :option="optionChild"
            :table-loading="loadingChild"
            :data="dataChild"
            ref="crudChild"
            v-model="formChild"
            :before-open="beforeOpenChild"
            :before-close="beforeCloseChild"
            @row-del="rowDelChild"
            @row-update="rowUpdateChild"
            @row-save="rowSaveChild"
            @search-change="searchChangeChild"
            @search-reset="searchResetChild"
            @selection-change="selectionChangeChild"
            @current-change="currentChangeChild"
            @size-change="sizeChangeChild"
            @refresh-change="refreshChangeChild"
            @on-load="onLoadChild"
          >
            <template slot="menuLeft">
              <el-button
                type="danger"
                size="small"
                icon="el-icon-delete"
                plain
                @click="handleDeleteChild"
                >删 除
              </el-button>
            </template>
            <template slot-scope="{ row }" slot="status">
              <el-tag>{{ row.status === 1 ? "否" : "是" }}</el-tag>
            </template>
          </avue-crud>
        </div>
      </el-card>
      <!-- </basic-container> -->
    </el-col>
  </el-row>
</template>

<script>
import {
  getParentList,
  getChildList,
  remove,
  update,
  add,
  addChild,
  updatechild,
  removechild,
} from "@/api/system/dictbiz";
import { optionParent, optionChild } from "@/option/system/dictbiz";

export default {
  data() {
    return {
      dictValue: "暂无",
      parentId: -1,
      formParent: {},
      formChild: {},
      selectionList: [],
      query: {},
      queryChild: {},
      loading: true,
      loadingChild: true,
      pageParent: {
        pageSize: 10,
        pageSizes: [10, 30, 50, 100, 200],
        currentPage: 1,
        total: 0,
      },
      pageChild: {
        pageSize: 10,
        pageSizes: [10, 30, 50, 100, 200],
        currentPage: 1,
        total: 0,
      },
      dataParent: [],
      dataChild: [],
      optionParent: optionParent,
      optionChild: optionChild,
      selectRow: {},
    };
  },
  computed: {
    ids() {
      let ids = [];
      this.selectionList.forEach((ele) => {
        ids.push(Number(ele.dictId));
      });
      return ids;
    },
    idchild() {
      let idchild = [];
      this.selectionList.forEach((ele) => {
        idchild.push(Number(ele.dictCode));
      });
      return idchild;
    },
  },
  methods: {
    initData() {
      this.$refs.crudChild.value.dictLabel = "";
      this.$refs.crudChild.value.dictType = this.parentId;
    },
    handleAdd(row) {
      this.$refs.crudChild.value.parentId = row.id;
      this.$refs.crudChild.option.column.filter((item) => {
        if (item.prop === "parentId") {
          item.value = row.id;
        }
      });
      this.$refs.crudChild.rowAdd();
    },
    rowSave(row, done, Loading) {
      add(row.dictName, row.dictType, row.remark).then((res) => {
        this.onLoadParent(this.pageParent);
        this.$message({
          type: "success",
          message: "操作成功!",
        });
        done();
      });
    },
    rowUpdate(row, index, done, loading) {
      update(row.dictId, row.dictName, row.dictType, row.remark).then(
        () => {
          this.onLoadParent(this.pageParent);
          this.$message({
            type: "success",
            message: "操作成功!",
          });
          done();
        },
        (error) => {
          window.console.log(error);
          loading();
        }
      );
    },
    rowDel(row) {
      this.$confirm("确定将选择数据删除?", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      }).then(() => {
        let id = [];
        id.push(row.dictId);
        remove(id).then((res) => {
          this.onLoadParent(this.pageParent);
          this.$message({
            type: "success",
            message: "操作成功!",
          });
          this.$refs.crud.toggleSelection();
        });
      });
    },
    handleRowClick(row) {
      this.query = {};
      this.parentId = row.dictType;
      this.dictValue = row.dictName;

      this.$refs.crudChild.value.dictType = row.dictType;
      this.$refs.crudChild.value.parentId = row.dictName;
      this.selectRow = row;
      this.onLoadChild(this.pageChild);
    },
    searchReset() {
      this.query = {};
      this.onLoadParent(this.pageParent);
    },
    searchChange(params, done) {
      this.query = params;
      this.pageParent.currentPage = 1;
      this.onLoadParent(this.pageParent, params);
      done();
    },
    selectionChange(list) {
      this.selectionList = list;
    },
    selectionClear() {
      this.selectionList = [];
      this.$refs.crud.toggleSelection();
    },
    handleDelete() {
      if (this.selectionList.length === 0) {
        this.$message.warning("请选择至少一条数据");
        return;
      }
      this.$confirm("确定将选择数据删除?", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      }).then(() => {
        remove(this.ids).then((res) => {
          this.onLoadParent(this.pageParent);
          this.$message({
            type: "success",
            message: "操作成功!",
          });
          this.$refs.crud.toggleSelection();
        });
      });
    },
    beforeOpen(done, type) {
      if (["edit", "view"].includes(type)) {
        // getDict(this.formParent.id).then(res => {
        //   this.formParent = res.data.data;
        // });
      }
      done();
    },
    currentChange(currentPage) {
      this.pageParent.currentPage = currentPage;
    },
    sizeChange(pageSize) {
      this.pageParent.pageSize = pageSize;
    },
    refreshChange() {
      this.onLoadParent(this.pageParent, this.query);
    },
    rowSaveChild(row, done, loading) {
      addChild(row.dictLabel, row.dictSort, row.dictValue, row.dictType).then(
        (res) => {
          this.onLoadChild(this.pageChild);
          this.$message({
            type: "success",
            message: "操作成功!",
          });
          done();
        }
      );
    },
    rowUpdateChild(row, index, done, loading) {
      updatechild(
        row.dictCode,
        row.dictLabel,
        row.dictSort,
        row.dictValue,
        row.dictType
      ).then((res) => {
        this.onLoadChild(this.pageChild);
        this.$message({
          type: "success",
          message: "操作成功!",
        });
        done();
      });
    },
    rowDelChild(row) {
      this.$confirm("确定将选择数据删除?", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      }).then(() => {
        let id = [];
        id.push(row.dictCode);
        removechild(id).then((res) => {
          this.onLoadChild(this.pageChild);
          this.$message({
            type: "success",
            message: "操作成功!",
          });
          this.$refs.crudChild.toggleSelection();
        });
      });
    },
    searchResetChild() {
      this.query = {};
      this.onLoadChild(this.pageChild);
    },
    searchChangeChild(params, done) {
      this.query = params;
      this.pageChild.currentPage = 1;
      this.onLoadChild(this.pageChild, params);
      done();
    },
    selectionChangeChild(list) {
      this.selectionList = list;
    },
    selectionClearChild() {
      this.selectionList = [];
      this.$refs.crudChild.toggleSelection();
    },
    handleDeleteChild() {
      if (this.selectionList.length === 0) {
        this.$message.warning("请选择至少一条数据");
        return;
      }
      this.$confirm("确定将选择数据删除?", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      }).then(() => {
        removechild(this.idchild).then((res) => {
          this.onLoadChild(this.pageChild);
          this.$message({
            type: "success",
            message: "操作成功!",
          });
          this.$refs.crudChild.toggleSelection();
        });
      });
    },
    beforeOpenChild(done, type) {
      if (["add", "edit"].includes(type)) {
        if (!this.dataParent.length) {
          this.$message.warning("请点击选择一项业务字典");
          return;
        }
        this.initData();
        this.$refs.crudChild.value.dictType = this.selectRow.dictType;
        setTimeout(() => {
          this.$refs.crudChild.value.parentId = this.selectRow.dictName;
        }, 500);
      }
      done();
    },
    beforeCloseChild(done) {
      this.$refs.crudChild.value.parentId = this.parentId;
      this.$refs.crudChild.option.column.filter((item) => {
        if (item.prop === "parentId") {
          item.value = this.parentId;
        }
      });
      done();
    },
    currentChangeChild(currentPage) {
      this.pageChild.currentPage = currentPage;
    },
    sizeChangeChild(pageSize) {
      this.pageChild.pageSize = pageSize;
    },
    refreshChangeChild() {
      this.onLoadChild(this.pageChild, this.queryChild);
    },
    onLoadParent(page, params = {}) {
      this.loading = true;
      // getParentList(
      //   page.currentPage,
      //   page.pageSize,
      //   Object.assign(params, this.query)
      // ).then(res => {
      //   const data = res.data.data;
      //   this.pageParent.total = data.total;
      //   this.dataParent = data.records;
      //   this.loading = false;
      //   this.selectionClear();
      // });
      getParentList(
        page.currentPage,
        page.pageSize,
        params.dictType,
        params.dictName
      ).then((res) => {
        const data = res.data;
        this.pageParent.total = data.total;
        this.dataParent = data.records;
        this.loading = false;
        this.selectionClear();
      });
    },
    onLoadChild(page, params = {}) {
      if (this.parentId == "") {
        this.dataChild = [];
        this.loadingChild = false;
        this.selectionClear();
      } else {
        this.loadingChild = true;
        getChildList(this.parentId, params.dictLabel).then((res) => {
          this.dataChild = res.data;
          this.loadingChild = false;
          this.selectionClear();
        });
      }
    },
  },
};
</script>
<style lang="scss" scoped>
.header-div {
  line-height: 65px;
}
</style>